<?php

class Register extends TPage
{
	public function onLoad($param)
	{
		parent::onLoad($param);
		if (!$this->Application->User->IsGuest)
			$this->Response->redirect($this->Service->ConstructUrl("Index"));

		if (!$this->IsPostBack)
		{
			$this->Master->pageHead->Title = $this->Application->Parameters["WEBSITE_TITLE"]." - Register";
			$this->Master->cssMenuRegister->Value = "current_menu_item";
			
			// Populate Age Group List
			$this->cboAgeSelector->DataSource = AgeGroupRecord::finder()->getAllItems();
			$this->cboAgeSelector->DataBind();
			
			// Populate Cuisine List
			$this->cboCuisineSelector->DataSource = CuisineRecord::finder()->getAllItems();
			$this->cboCuisineSelector->DataBind();
		}

	}
	
	protected function btnSubmit_Clicked($sender, $param)
	{
		if ($this->IsValid)
		{
			try
			{
				$userRecord = new UserRecord;
				$userRecord->UserName = $this->txtUserName->SafeText;
				$userRecord->Email = $this->txtEmail->SafeText;
				$userRecord->Password = md5($this->txtPassword->SafeText);			
				$userRecord->AgeGroup = $this->cboAgeSelector->SelectedValue;
				$userRecord->PreferredCuisine = $this->cboCuisineSelector->SelectedValue;
				$userRecord->Status = 1; // status: 0 inactive, 1 active, 2 admin -1 deleted
				$userRecord->Hash = md5(uniqid(time())).'.'.strtolower(array_pop(explode('.',$this->txtEmail->SafeText)));
				$userRecord->IPAddress = $this->Request->UserHostAddress;
				$userRecord->ReceiveUpdates = "N";
				if ($this->chkReceiveUpdate->Checked)					
					$userRecord->ReceiveUpdates = "Y";
				$userRecord->CreateBy = $userRecord->ModifyBy = 0; // 0: self created	
				$userRecord->CreateDate = $userRecord->ModifyDate = time();								
				$userRecord->save();
				
				// if register successsful, send email
				$emailer = $this->Application->getModule('mailer');
				$email = $emailer->createNewEmail("RegisterSuccess");
				$email->HtmlContent->findControl("USER_NAME")->Text = $userRecord->UserName;				
				/*$activationLink = $this->Request->BaseUrl;
				if(stristr($this->Request->BaseUrl, 'localhost'))
					$activationLink = $activationLink . "/latenightdine";					
				$activationLink = $activationLink . "/activate-account.html?email=" . $user->Email . "&code=" . $user->Hash;
				$email->HtmlContent->findControl("ACTIVATION_LINK")->Text = $activationLink;*/
				$receiver = new TEmailAddress;
				$receiver->Field = TEmailAddressField::Receiver;
				$receiver->Address = $userRecord->Email;
				$receiver->Name = $userRecord->UserName;
				$email->getEmailAddresses()->add($receiver);
				$emailer->send($email);	
								
				$this->Application->getModule("auth")->login($this->txtEmail->SafeText,$this->txtPassword->SafeText);
				$this->Response->redirect($this->Service->ConstructUrl("Index", array('m'=>'cu')));										
			}
			catch(TException $e)
			{
				$this->Notice->Type = UserNoticeType::Error;
				$this->Notice->Text = $e;
			}
		}
		else
		{
			$pass = $this->txtPassword->Text;
			$this->txtPassword->TextMode="Password";
			$this->txtConfirmPassword->TextMode="Password";
			$this->txtPassword->setAttribute("value", $pass);
			$this->txtConfirmPassword->setAttribute("value", $pass);
			$this->capt->regenerateToken();
			$this->txtCapt->Text="";
		}
	}
	
	protected function uniqueCheck_ServerValidated($sender, $param)
	{
		if ($param->Value != '')
		{
			$criteria = new TActiveRecordCriteria;
			$criteria->Condition = "user_email = :email";
			$criteria->Parameters[":email"] = $param->Value;
			$param->IsValid = !(UserRecord::finder()->find($criteria) instanceof UserRecord);
		}
	}
}

?>